package com.infore.dataReceiving.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.infore.dataReceiving.core.chain.Water2018InstructionsChain;
import com.infore.dataReceiving.core.enums.Water2018InstructionsRegularExpressionEnum;
import com.infore.dataReceiving.core.filter.instructions.CheckDataFilter;
import com.infore.dataReceiving.core.filter.instructions.EquipmentInfoFilter;
import com.infore.dataReceiving.core.filter.instructions.HourDataFilter;
import com.infore.dataReceiving.core.filter.instructions.RecoveryDataFilter;
import com.infore.dataReceiving.core.filter.instructions.ResponseFilter;
import com.infore.dataReceiving.core.filter.instructions.ResultFilter;
import com.infore.dataReceiving.core.filter.instructions.RtdFilter;
import com.infore.dataReceiving.core.filter.instructions.SpanCheckDataFilter;
import com.infore.dataReceiving.core.filter.instructions.ZeroCheckDataFilter;
import com.infore.dataReceiving.dto.WaterInstructionsDto;

import io.netty.channel.ChannelHandlerContext;

public class Water2018InstructionsHandleUtil {
	
	public static Map<String, ChannelHandlerContext> cMap = new HashMap<>();
	
	private static final ThreadLocal<SimpleDateFormat> sdfThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
	
	public static WaterInstructionsDto getWaterInstructions(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.matches();
		WaterInstructionsDto waterInstructionsDto = null;
		if(_flag) {
			waterInstructionsDto = new WaterInstructionsDto();
			waterInstructionsDto.setHead("##");
			waterInstructionsDto.setDataLength(m.group(1)==null?null:Integer.valueOf(m.group(1)));
			waterInstructionsDto.setDatas(m.group(2));
			waterInstructionsDto.setQn(m.group(3));
			waterInstructionsDto.setSt(m.group(4));
			waterInstructionsDto.setCn(m.group(5));
			waterInstructionsDto.setPw(m.group(6));
			waterInstructionsDto.setMn(m.group(7));
			waterInstructionsDto.setFlag(Integer.valueOf(m.group(8)));
			waterInstructionsDto.setPnum(m.group(10)==null?null:Integer.valueOf(m.group(10)));
			waterInstructionsDto.setPno(m.group(12)==null?null:Integer.valueOf(m.group(12)));
			waterInstructionsDto.setDataTime(m.group(14));
			waterInstructionsDto.setFactorData(m.group(15));
			waterInstructionsDto.setBeginTime(m.group(16));
			waterInstructionsDto.setEndTime(m.group(17));
			waterInstructionsDto.setQnRtn(m.group(18));
			waterInstructionsDto.setExeRtn(m.group(19));
			waterInstructionsDto.setCrc(m.group(20));
			waterInstructionsDto.setEnd("\r\n");
		}
		return waterInstructionsDto;
	}
	
	public static Map<String, String> getInstructions_Flag(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Flag.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		Map<String, String> map = null;
		if(_flag) {
			map = new HashMap<String, String>();
			map.put("factorCode", m.group(1));
			map.put("flag", m.group(2));
		}
		return map;
	}
	
	public static String getInstructions_Rtd(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Rtd.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_Avg(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Avg.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_Check(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Check.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_StandardValue(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_StandardValue.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_WaterTime(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_WaterTime.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_Water(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Water.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_Chroma(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Chroma.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_Volume(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_Volume.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_DVolume(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_DVolume.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_SpanValue(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_SpanValue.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static String getInstructions_PolId(String msg) {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_PolId.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static Map<String,String> getInstructions_EquipmentInfo(String msg) throws Exception {
		Pattern p=Pattern.compile(Water2018InstructionsRegularExpressionEnum.Water2018_Instructions_EquipmentInfo.getParrtern()); 
		Matcher m=p.matcher(msg);
		Map<String,String> dataMap = new HashMap<>();
		while(m.find()) {
			String code = m.group(1);
			String value = null;
			String value1 = m.group(3);
			String value2 = m.group(4);
			if(value1!=null) {
				value = value1;
			}
			if(value2!=null) {
				value = value2;
			}
			dataMap.put(code, value);
		}
		return dataMap;
	}
	
	public static Timestamp getTimestamp(String dataTime) {
		if(!dataTime.contains("-")) {
			dataTime = dataTime.substring(0,4)+"-"+dataTime.substring(4,6)+"-"+dataTime.substring(6,8)
			+" "+dataTime.substring(8,10)+":"+dataTime.substring(10,12)+":"+dataTime.substring(12,14);
		}
		SimpleDateFormat sdf = sdfThreadLocal.get();
		Date date = null;
		try {
		date = sdf.parse(dataTime);
		} catch (ParseException e) {
		e.printStackTrace();
		}
		return new Timestamp(date.getTime());
	}
	
	public static Water2018InstructionsChain getMessageFilterChain() {
		Water2018InstructionsChain chain = new Water2018InstructionsChain();
		chain.addFilter(new RtdFilter());
		chain.addFilter(new HourDataFilter());
		chain.addFilter(new CheckDataFilter());
		chain.addFilter(new RecoveryDataFilter());
		chain.addFilter(new ZeroCheckDataFilter());
		chain.addFilter(new SpanCheckDataFilter());
		chain.addFilter(new EquipmentInfoFilter());
		chain.addFilter(new ResponseFilter());
		chain.addFilter(new ResultFilter());
		return chain;
	}
	
	public static String getDataLengthString(String dataLength) {
		if(dataLength.length()==1) {
			dataLength="000"+dataLength;
		}else if(dataLength.length()==2) {
			dataLength="00"+dataLength;
		}else if(dataLength.length()==3) {
			dataLength="0"+dataLength;
		}
		return dataLength;
	}

}
